﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;

namespace Mammoo.Ajax
{
    /// <summary>
    /// Ajax通信返回执行状态
    /// </summary>
    public enum ReturnStatus
    {
        /// <summary>
        /// Session过期
        /// </summary>
        TimeOut = -1,

        /// <summary>
        /// 失败
        /// </summary>
        Fail = 0,

        /// <summary>
        /// 成功
        /// </summary>
        Success = 1,
    }

    /// <summary>
    /// Ajax通信帮助类
    /// </summary>
    public static class AjaxService
    {
        #region AJAX通信

        #region 写AJAX基础
        /// <summary>
        /// 写ajax通信
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static string WriteAjax(AjaxResult result)
        {
            return JsonHelper.Convert2Json(result);
        }

        /// <summary>
        /// 写AJAX通信
        /// </summary>
        /// <param name="status">ReturnStatus TimeOut,Fail,Success</param>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteAjax(ReturnStatus status, object msg, object data)
        {
            Hashtable ht = new Hashtable();
            ht.Add("success", status);
            ht.Add("msg", msg);
            ht.Add("data", data);
            return JsonHelper.Convert2Json(ht);
        }

        /// <summary>
        /// 写AJAX通信，执行成功！
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteSucc(object msg, object data)
        {
            return WriteAjax(ReturnStatus.Success, msg, data);
        }

        /// <summary>
        /// 写AJAX通信，执行失败！
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteFail(object msg, object data)
        {
            return WriteAjax(ReturnStatus.Fail, msg, data);
        }

        /// <summary>
        /// 写AJAX通信，执行过期
        /// </summary>
        /// <returns></returns>
        public static string WriteTimeOut()
        {
            return WriteAjax(ReturnStatus.TimeOut, "lang.util.timeOut", "");
        }

        /// <summary>
        /// 写AJAX通信，执行过期，回写上下文
        /// </summary>
        /// <param name="context">会话上下文</param>
        public static void WriteTimeOut(HttpContext context)
        {
            context.Response.Write(WriteTimeOut());
        }
        #endregion

        #region 写AJAX保存
        /// <summary>
        /// 写AJAX通信，保存成功！
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteSaveSucc(object data)
        {
            return WriteSucc("lang.util.saveSucc", data);
        }

        /// <summary>
        /// 写AJAX通信，保存成功，执行上下文回写！
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="context">会话上下文</param>
        public static void WriteSaveSucc(object data, HttpContext context)
        {
            context.Response.Write(WriteSaveSucc(data));
        }

        /// <summary>
        /// 写AJAX通信，保存失败！
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteSaveFail(object data)
        {
            return WriteFail("lang.util.saveFail", data);
        }

        /// <summary>
        /// 写AJAX通信，保存失败，执行上下文回写！
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="context">会话上下文</param>
        public static void WriteSaveFail(object data, HttpContext context)
        {
            context.Response.Write(WriteSaveFail(data));
        }
        #endregion

        #region 写AJAX加载
        /// <summary>
        /// 写AJAX通信，加载成功！
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteLoadSucc(object data)
        {
            return WriteSucc("lang.util.loadSucc", data);
        }

        /// <summary>
        /// 写AJAX通信，加载成功，执行上下文回写！
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="context">会话上下文</param>
        /// <returns></returns>
        public static void WriteLoadSucc(object data, HttpContext context)
        {
            context.Response.Write(WriteLoadSucc(data));
        }

        /// <summary>
        /// 写AJAX通信，加载失败！
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteLoadFail(object data)
        {
            return WriteSucc("lang.util.loadFail", data);
        }

        /// <summary>
        /// 写AJAX通信，加载失败，执行上下文回写！
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="context">会话上下文</param>
        /// <returns></returns>
        public static void WriteLoadFail(object data, HttpContext context)
        {
            context.Response.Write(WriteLoadSucc(data));
        }
        #endregion

        #region 写AJAX删除
        /// <summary>
        /// 写AJAX通信，删除成功！
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteDelSucc(object data)
        {
            return WriteSucc("lang.util.delSucc", data);
        }

        /// <summary>
        /// 写AJAX通信，删除成功，执行上下文回写！
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="context">会话上下文</param>
        /// <param name="context"></param>
        public static void WriteDelSucc(object data, HttpContext context)
        {
            context.Response.Write(WriteDelSucc(data));
        }

        /// <summary>
        /// 写AJAX通信，删除失败！
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static string WriteDelFail(object data)
        {
            return WriteFail("lang.util.delFail", data);
        }

        /// <summary>
        /// 写AJAX通信，删除失败，执行上下文回写！
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="context">会话上下文</param>
        /// <param name="context"></param>
        public static void WriteDelFail(object data, HttpContext context)
        {
            context.Response.Write(WriteDelFail(data));
        }
        #endregion

        #endregion
    }
}
